// pages/course/course.js 
var app = getApp() 
Page({ 
 
  /** 
   * 页面的初始数据 
   */ 
  data: { 
    navState:0, 
    day:[], 
    kebiaoBackground:"",
    scrollLeft:""
  }, 
 
  /* 
  res.data[0].RECORDS[0].courses[1].day：星期 2   
  */ 
  onShow: function (options) { 
    console.log(app.globalData.kebiaoBackground) 
    var that = this 
    that.setData({ 
      kebiaoBackground:app.globalData.kebiaoBackground 
    }) 
    wx.cloud.init() 
    const db = wx.cloud.database() 
    db.collection('course').get({ 
      success(res){ 
        that.setData({ 
          day: res.data[0].RECORDS[0].courses 
        }) 
      } 
    }) 
  }, 
  changeTab: function (params) { 
    // 设置 
    var that = this; 
    let {week} = this.data; 
    //箭头函数左边是传入的对象右边是对该对象的操作 
    console.log(week) 
    week.forEach((item,i) => i===params?item.isActive=true:item.isActive=false); 
 
    //将修改后的数组赋值回去 
    that.setData({ 
      week 
    }) 
  }, 
 
  scroll_tap: function (e) { 
    console.log(e.currentTarget.dataset) 
    //获取当前被点击的标签的 id 
    //被点击的 item 的序号：e.currentTarget.dataset.index + 1 
    var that = this; 
    const {index} = e.currentTarget.dataset; 
    //获取data中的数组 
    let {week} = this.data; 
    //箭头函数左边是传入的对象右边是对该对象的操作 
    console.log(week) 
    week.forEach((item,i) => i===index?item.isActive=true:item.isActive=false); 
 
    //将修改后的数组赋值回去 
    that.setData({ 
      week 
    }) 
 
    console.log(week) 
  }, 
  tabSelect(e) { 
    this.setData({ 
      TabCur: e.currentTarget.dataset.id, 
      scrollLeft: (e.currentTarget.dataset.id-1)*60 
    }) 
  }, 
  bindchange:function (e) { 
    // 获取当前页面的序号 
    console.log(e.currentTarget.detail) 
    //console.log(this.data.course.Mon) 
    var that = this; 
    let key = e.detail.current 
    console.log(key) 
    this.setData({ 
      navState:key 
    }) 
  }, 
  navSwitch: function (e) { 
    console.log(e.currentTarget.dataset.index) 
    this.setData({ 
      navState:e.currentTarget.dataset.index 
    }) 
    /*scroll-view点击项自动居中 */
    let offsetLeft = e.currentTarget.offsetLeft
    this.setData({
      scrollLeft: offsetLeft - this.data.scrollViewWidth/2
    })
  }, 
  onReady: function(){
    /*scroll-view点击项自动居中 */
    wx.createSelectorQuery().select('.scroll-view').boundingClientRect((rect)=>{
      this.data.scrollViewWidth = Math.round(rect.width)
    }).exec()
  }
})